package com.softme.trade.control;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.DualListModel;
import org.springframework.transaction.annotation.Transactional;

import com.softme.trade.model.Firm;
import com.softme.trade.model.Role;
import com.softme.trade.model.User;
import com.softme.trade.model.dao.FirmDAO;
import com.softme.trade.model.dao.RoleDAO;
import com.softme.trade.model.dao.UserDAO;
import com.softme.trade.util.BeanFactory;
import com.softme.trade.view.components.ColumnModel;
import com.softme.trade.view.components.DataTableUtils;

@ViewScoped
@ManagedBean
public class UserController implements  Serializable
{
	private static final long serialVersionUID = 1L;
	
	private User user = new User();
	
	private Role selectedRole = new Role();

	private List<User> listUsers;
	private List<Firm> listFirms;
	private List<Role> listRoles;
	
	private DualListModel<Firm> dualListFirms;
	
	private UserDAO userDAO;
	private FirmDAO firmDAO;
	private RoleDAO roleDAO;

	public UserController()
	{
		this.updateScreen();
	}
	
	/**
	 * Limpa os campos input e atualiza a lista de usu�rios cadastrados
	 */
	private void updateScreen()
	{
		this.setListUsers(this.getUserDAO().loadAll());
		this.setListFirms(this.getFirmDAO().loadAll());
		this.setListRoles(this.getRoleDAO().loadAll());
		
		dualListFirms = new DualListModel<Firm>();
		
		//seta a lista de todas as empresa no source
		dualListFirms.setSource(this.getListFirms());
	}

	/**
	 * Grava novo registro ou atualiza um registro
	 */
	public void save()
	{
		//seta as empresas selecionadas no usuário
		this.getUser().setFirms(new HashSet<Firm>(dualListFirms.getTarget()));
		this.getUserDAO().saveOrUpdate(this.getUser());
		this.updateScreen();
	}

	/**
	 * Exclui um registro da tabela usuario
	 */
	public void delete()
	{
		this.getUserDAO().delete(this.getUser());
		this.updateScreen();
	}
	
	public void create()
	{
		this.user = new User();
		this.updateScreen();
	}

	public List<User> getListUsers()
	{
		return listUsers;
	}

	public void setListUsers(List<User> listUsers)
	{
		this.listUsers = listUsers;
	}

	public User getUser()
	{
//		this.getDualListFirms().setTarget(new ArrayList<Firm>(this.user.getFirms()));
		
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	public List<Firm> getListFirms()
	{
		return listFirms;
	}

	public void setListFirms(List<Firm> listFirms)
	{
		this.listFirms = listFirms;
	}
	
	public List<Role> getListRoles()
	{
		return listRoles;
	}

	public void setListRoles(List<Role> listRoles)
	{
		this.listRoles = listRoles;
	}

	public Role getSelectedRole()
	{
		return selectedRole;
	}

	public void setSelectedRole(Role selectedRole)
	{
		this.selectedRole = selectedRole;
	}

	public DualListModel<Firm> getDualListFirms()
	{
		return dualListFirms;
	}

	public void setDualListFirms(DualListModel<Firm> dualListFirms)
	{
		this.dualListFirms = dualListFirms;
	}

	public UserDAO getUserDAO()
	{
		if (this.userDAO == null || this.userDAO.getHibernateTemplate() == null)
			this.userDAO = (UserDAO) BeanFactory.getBean("userDAO", UserDAO.class);
		
		return this.userDAO;
	}
	
	public FirmDAO getFirmDAO()
	{
		if (this.firmDAO == null || this.firmDAO.getHibernateTemplate() == null)
			this.firmDAO = (FirmDAO) BeanFactory.getBean("firmDAO", FirmDAO.class);
		
		return this.firmDAO;
	}
	
	public RoleDAO getRoleDAO()
	{
		if (this.roleDAO == null || this.roleDAO.getHibernateTemplate() == null)
			this.roleDAO = (RoleDAO) BeanFactory.getBean("roleDAO", RoleDAO.class);
		
		return this.roleDAO;
	}
}
